jQuery.fn.tagName = function(){
	if(1 === this.length){
		return this[0].tagName.toLowerCase();
	} else{
		var tagNames = [];
		this.each(function(i, el){
			tagNames[i] = el.tagName.toLowerCase();
		});
		return tagNames;
	}
};

var makeContent = function(input) {
	return input.val();
}
var makeInput = function(block) {
	switch(block.attr("class")) {
	case "inline-text":
	default:
		var input = $("<input>").attr({
			"type": "text",
			"name" : block.attr("name") || "content"
		}).val(block.html()); 
	break;
	case "inline-textarea":
		var input = $("<textarea>").attr("name", block.attr("name") || "content").text(block.html());
	break;
	}
	return $("<div>").append(input);
}

jQuery.fn.abeInline = function(options) {
	return this.each(function() {
		var $this = $(this);
		$this.settings = jQuery.extend({
			ajax : null,
			beforeConvert : function() {return true;},
			beforeSave : function(oldValue, newValue) {return true;},
			afterSave : function(oldValue, newValue) {},
			noSave : function(oldValue, newValue) {},
			submitAll : false
		}, options);
		
		$this.dblclick(function(){
			$this.hide();
			var initial = $this.html();
			var inputblock = makeInput($this);
			var input = $(":input:first", inputblock);
			var save = $("<button>").text("Сохранить").click(function() {
				content = makeContent(input);
				doSave = $this.settings.beforeSave(initial, content);
				if (doSave) {
					$this.html(content);
					$this.show();
					inputblock.remove();
					if ($this.settings.ajax) {
						var data = "&"+input.attr("name")+"="+content
						var ajax = jQuery.extend({},  $this.settings.ajax);
						ajax.data = (ajax.data? ajax.data : "" )+data
						$.ajax(ajax)
					}
					$this.settings.afterSave(initial, content);
				} else {
					$this.settings.noSave(initial, content);
				}
			})
			var cencel =  $("<button>").text("Отмена").click(function() {
				$this.show();
				inputblock.remove();
			});
			$this.after(inputblock.append(save).append(cencel));
			input.focus();
		});

	});
}

jQuery.fn.abeInlineForm = function(options) {
	var inlineAll = $(this);
	var initial = [];
	var inputblock = [];
	var input = [];	

	var settings = jQuery.extend({
		ajax : null,
		beforeConvert : function() {return true;},
		beforeSave : function(oldValue, newValue) {return true;},
		afterSave : function(oldValue, newValue) {},
		noSave : function(oldValue, newValue) {},
		submitAll : false
	}, options);
	return inlineAll.dblclick(function(){
		var save = $("<button>").text("Сохранить").click(function() {
			var content  = [];
			$(input).each(function (index) {
				content[index] = makeContent(input[index]);
			});
			doSave = settings.beforeSave(initial, content);
			if (doSave) {
				inlineAll.each(function (index) {
					$(this).html(content[index]);
					$(this).show();
					inputblock[index].remove();
				});
				if (settings.ajax) {
					var tmpform = $("<form>");
					$(input).each(function (index) {
						tmpform.append(input[index]);
					})
					var data = tmpform.serialize();
					var ajax = jQuery.extend({},  settings.ajax);
					ajax.data = (ajax.data? ajax.data : "" )+data
					$.ajax(ajax)
				}
				settings.afterSave(initial, content);
			} else {
				settings.noSave(initial, content);
			}
		})
		var cencel = $("<button>").text("Отмена").click(function(){
			$(inlineAll).show();
			$(inputblock).each(function () {
				$(this).remove();
			});
			save.remove();
			cencel.remove();
		});
		inlineAll.hide();
		inlineAll.each(function (index) {
			initial[index] = $(this).html();
			inputblock[index] = makeInput($(this));
			input[index] = $(":input:first", inputblock[index])
			$(this).after(inputblock[index]);
		});
		$(inputblock[inputblock.length-1]).append(save).append(cencel);
	});
}
